package com.web.mall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.mall.pojo.PageReq;
import com.web.mall.pojo.PageResp;
import com.web.mall.pojo.Result;
import com.web.mall.pojo.TbBrand;
import com.web.mall.service.BrandManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BrandManagerController {

    @Autowired
    private BrandManagerService brandManagerService;

    @GetMapping("/BrandListByPage")
    public PageResp<TbBrand> listByPage(PageReq pageReq){
        PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
        Page<TbBrand> page = (Page<TbBrand>) brandManagerService.queryBrand();

        PageResp pageResp = new PageResp();
        pageResp.setTotal(page.getTotal());
        pageResp.setList(page.getResult());

        return pageResp;
    }

    @DeleteMapping("/deleteBrand/{id}")
    public Result deleteBrand(@PathVariable Long id){
        boolean b= this.brandManagerService.deleteBrand(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/saveBrand")
    public Result save(@RequestBody TbBrand brand){
        System.out.println(brand.getId());
        boolean b= this.brandManagerService.saveBrand(brand);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/findBrandList")
    public List<TbBrand> findBrandList(){
        return brandManagerService.findBrandList();
    }

}
